package com.bdqn.petmalldemo.controller;


import com.bdqn.petmalldemo.bo.CategoryBo;
import com.bdqn.petmalldemo.common.ResultCodeEnum;
import com.bdqn.petmalldemo.common.ReturnResult;
import com.bdqn.petmalldemo.common.TokenCache;
import com.bdqn.petmalldemo.entity.Category;
import com.bdqn.petmalldemo.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lenovo
 * @since 2020-02-10
 */
@Api(tags = "商品分类控制器")
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "商品分类列表")
    @PostMapping(value = "getAll")
    public ReturnResult getAll(){
        List<Category> categoryList = categoryService.getAll();
        return ReturnResult.ok().data(categoryList);
    }
    @ApiOperation(value = "新增商品分类")
    @PostMapping(value = "/add")
    public ReturnResult addCategory(
            CategoryBo categoryBo
    ){
        //校验token
        String jsonUser = TokenCache.get(categoryBo.getToken());
        if (StringUtils.isEmpty(jsonUser)) {
            return ReturnResult.error().code(ResultCodeEnum.TOKEN_INVALID.getCode())
                    .msg(ResultCodeEnum.TOKEN_INVALID.getMsg());
        }
        //参数的非空校验
        if (StringUtils.isEmpty(categoryBo.getName())) {
            return  ReturnResult.error().code(ResultCodeEnum.CATEGORY_NAME_NULL.getCode())
                    .msg(ResultCodeEnum.CATEGORY_NAME_NULL.getMsg());
        }
        if (StringUtils.isEmpty(categoryBo.getParentId())) {
            return  ReturnResult.error().code(ResultCodeEnum.CATEGORY_PARENT_ID_NULL.getCode())
                    .msg(ResultCodeEnum.CATEGORY_PARENT_ID_NULL.getMsg());
        }
        if (StringUtils.isEmpty(categoryBo.getOrderNo())) {
            return  ReturnResult.error().code(ResultCodeEnum.CATEGORY_ORDER_NO_NULL.getCode())
                    .msg(ResultCodeEnum.CATEGORY_ORDER_NO_NULL.getMsg());
        }
        categoryService.addOrupdateCategory(categoryBo);
        return ReturnResult.ok();
    }
    @ApiOperation(value = "商品分类编辑前的查询")
    @PostMapping(value = "get")
    public ReturnResult getCategoryById(
            @ApiParam(name = "token",value = "当前登录用户的Token",required = true)
            String token,
            @ApiParam(name = "id",value ="需要被修改的商品分类",required = true)
            Integer id
    ){
        //先校验参数不能为空
        if (StringUtils.isEmpty(token)) {
            return  ReturnResult.error().code(ResultCodeEnum.TOKEN_NULL.getCode())
                    .msg(ResultCodeEnum.TOKEN_NULL.getMsg());
        }
        if (StringUtils.isEmpty(id)) {
            return  ReturnResult.error().code(ResultCodeEnum.ID_NULL.getCode())
                    .msg(ResultCodeEnum.ID_NULL.getMsg());
        }
        //token是否失效
        String jsonUser = TokenCache.get(token);
        if (StringUtils.isEmpty(jsonUser)) {
            return ReturnResult.error().code(ResultCodeEnum.TOKEN_INVALID.getCode())
                    .msg(ResultCodeEnum.TOKEN_INVALID.getMsg());
        }
        Category category = categoryService.getById(id);
        if (category==null) {
            return  ReturnResult.error().code(ResultCodeEnum.OBJECT_NULL.getCode())
                    .msg(ResultCodeEnum.OBJECT_NULL.getMsg());
        }
        return ReturnResult.ok().data(category);
    }
    @ApiOperation(value = "修改商品分类信息")
    @PostMapping("/update")
    public ReturnResult update(CategoryBo categoryBo){
        //校验token
        String jsonUser = TokenCache.get(categoryBo.getToken());
        if (StringUtils.isEmpty(jsonUser)) {
            return ReturnResult.error().code(ResultCodeEnum.TOKEN_INVALID.getCode())
                    .msg(ResultCodeEnum.TOKEN_INVALID.getMsg());
        }
        //参数的非空校验
        if (StringUtils.isEmpty(categoryBo.getName())) {
            return  ReturnResult.error().code(ResultCodeEnum.CATEGORY_NAME_NULL.getCode())
                    .msg(ResultCodeEnum.CATEGORY_NAME_NULL.getMsg());
        }
        if (StringUtils.isEmpty(categoryBo.getParentId())) {
            return  ReturnResult.error().code(ResultCodeEnum.CATEGORY_PARENT_ID_NULL.getCode())
                    .msg(ResultCodeEnum.CATEGORY_PARENT_ID_NULL.getMsg());
        }
        if (StringUtils.isEmpty(categoryBo.getOrderNo())) {
            return  ReturnResult.error().code(ResultCodeEnum.CATEGORY_ORDER_NO_NULL.getCode())
                    .msg(ResultCodeEnum.CATEGORY_ORDER_NO_NULL.getMsg());
        }
        if (StringUtils.isEmpty(categoryBo.getId())) {
            return  ReturnResult.error().code(ResultCodeEnum.ID_NULL.getCode())
                    .msg(ResultCodeEnum.ID_NULL.getMsg());
        }
        categoryService.addOrupdateCategory(categoryBo);
        return ReturnResult.ok();
    }

    @ApiOperation(value = "获取热门商品分类")
    @PostMapping(value = "getHotList")
    public ReturnResult getHotList(){
        List<Category> categoryList = categoryService.getAll();
            return ReturnResult.ok().data(categoryList);
    }

}

